//
//  VC_BoughtRecord.m
//  yjw-winner
//
//  Created by Kae on 2018/9/6.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_BoughtRecord.h"
#import "Cell_BoughtRecord.h"

static NSString *identifier = @"bought_record_identifier";
@interface VC_BoughtRecord ()<UITableViewDelegate, UITableViewDataSource> {
    UITableView *_tableView;
}

@property (nonatomic, strong) NSArray *btns;
@property (nonatomic, strong) UIView *siginView;

@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation VC_BoughtRecord

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@"",
                       @"",
                       @""
                       ];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"购买记录";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = MJRefreshColor(242, 242, 242);
    [self initButtons];
    [self initSiginView];
    [self initTableView];
}

/**
 初始化tableView
 */
- (void)initTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.mj_w, self.view.mj_h - 50 - 64) style:UITableViewStylePlain];
    _tableView.backgroundColor = MJRefreshColor(242, 242, 242);
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Cell_BoughtRecord class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    [self.view addSubview:_tableView];
}
- (void)initButtons {
    NSArray *titles = @[@"商学院", @"读书会", @"精品课"];
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:titles.count];
    for (int i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:MJRefreshColor(220, 188, 151) forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(segmentButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [mArr addObject:button];
        button.frame = CGRectMake(self.view.bounds.size.width / 3 * i, self.view.frame.origin.y, self.view.bounds.size.width / 3, 50);
        button.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:button];
        if (i == 0) {
            button.selected = YES;
        }
    }
    self.btns = [NSArray arrayWithArray:mArr];
}
- (void)initSiginView {
    self.siginView = [[UIView alloc] init];
    self.siginView.backgroundColor = MJRefreshColor(220, 188, 151);
    UIButton *btn = self.btns.firstObject;
    CGFloat w = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}].width;
    self.siginView.bounds = CGRectMake(0, 0, w, 1);
    [self.view addSubview:self.siginView];
    self.siginView.center = CGPointMake(self.view.bounds.size.width / 6, 50);
}


#pragma mark --mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Cell_BoughtRecord *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[Cell_BoughtRecord alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120.0f;
}



#pragma mark --mark EVENT

- (void)segmentButtonClick:(UIButton *)sender {
    if (sender.isSelected) {
        return;
    }
    for (UIButton *btn in self.btns) {
        btn.selected = NO;
    }
    sender.selected = YES;
    [self segmentDidselectedWithIndex:sender.tag - 100];
}


#pragma mark --mark 业务逻辑

- (void)segmentDidselectedWithIndex:(NSInteger)index {
    CGPoint center = CGPointMake((1 + index * 2) * self.view.mj_w / 6, 50);
    [UIView animateWithDuration:0.2 animations:^{
        self.siginView.center = center;
    }];
}
@end
